package invest.data.getdata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.XueqiuCookie;
import invest.pojo.datapojo.KlineHistory;
import invest.pojo.datapojo.StockMinuteKline;
import invest.utils.HttpRequest;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//获取股票分钟数据
public class StockMinuteKlineData implements Data<List<KlineHistory>> {
    private String code="";
    //    private String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SZ128033&begin=1617021934480&period=day&type=before&count=-100000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";
    private String pre = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=";
    private String p1 = "&begin=";
    private String after = "&period=1m&type=before&count=-5000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";

    public StockMinuteKlineData(String code) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(code);
        if (m.matches()) {
            flag = true;
        }
        if (flag) {
            if (code.length() <= 5) {
                this.code = code;
            } else {
                if (code.startsWith("6")) {
                    this.code = "SH" + code;
                } else {
                    this.code = "SZ" + code;
                }
            }
        } else {
            this.code = code;
        }
    }

    @Override
    public List<KlineHistory> getData() throws IOException {
        String cookie = XueqiuCookie.getXueqiuCookie();
        Long timeMillis = System.currentTimeMillis(); // 当前时间戳
        String url = pre + code + p1 + String.valueOf(timeMillis) + after;
        String result = HttpRequest.sendGet(url, cookie);
        JSONObject jb = JSONObject.parseObject(result);
        JSONObject data = jb.getJSONObject("data");
        JSONArray item = data.getJSONArray("item");
        List<KlineHistory> stockMinuteKlines = new ArrayList<>();
        for (int i = 0; i <item.size() ; i++) {
            JSONArray jsonArray=item.getJSONArray(i);
            KlineHistory klineHistory = new KlineHistory();
            klineHistory.setDate(new Date((Long) jsonArray.get(0)));
            klineHistory.setVolume((Integer) jsonArray.get(1));
            klineHistory.setOpen(new Double(((BigDecimal) jsonArray.get(2)).doubleValue()));
            klineHistory.setHigh(new Double(((BigDecimal) jsonArray.get(3)).doubleValue()));
            klineHistory.setLow(new Double(((BigDecimal) jsonArray.get(4)).doubleValue()));
            klineHistory.setClose(new Double(((BigDecimal) jsonArray.get(5)).doubleValue()));
            klineHistory.setChg(new Double(((BigDecimal) jsonArray.get(6)).doubleValue()));
            klineHistory.setPercent(new Double(((BigDecimal) jsonArray.get(7)).doubleValue()));
            klineHistory.setTurnoverrate(new Double(((BigDecimal) jsonArray.get(8)).doubleValue()));
            klineHistory.setAmount(new Double(((BigDecimal) jsonArray.get(9)).doubleValue()));
            stockMinuteKlines.add(klineHistory);
        }
        return stockMinuteKlines;
    }

    public static void main(String[] args) throws IOException {
        StockMinuteKlineData stockMinuteKlineData = new StockMinuteKlineData("IMV");
        List<KlineHistory> klineHistories = stockMinuteKlineData.getData();
        System.out.println(1);
    }
}
